package com.yl.ylnews.base.imp.menu;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.viewpagerindicator.TabPageIndicator;
import com.yl.ylnews.R;
import com.yl.ylnews.base.BaseMenuDetailPager;
import com.yl.ylnews.base.imp.menu.tab.BaseTabPager;
import com.yl.ylnews.global.GlobalConstants;
import com.yl.ylnews.view.TopNewsViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/20 0020.
 */

public class NewsMenuDetailPager extends BaseMenuDetailPager {

    private TabPageIndicator mTabPageIndicator;
    private ViewPager mNews_menu;
    private ImageButton mBtnnext;
    private List<BaseTabPager> mTabPagers;

    public NewsMenuDetailPager(Activity mActivity) {
        super(mActivity);
    }

    @Override
    public View initView() {
        View view = View.inflate(mActivity, R.layout.pager_news_menu_detail, null);
        mTabPageIndicator = (TabPageIndicator) view.findViewById(R.id.mTabPageIndicator);
        mBtnnext = (ImageButton) view.findViewById(R.id.btn_next);
        mNews_menu = (TopNewsViewPager) view.findViewById(R.id.vp_news_menu);
        return view;
    }



    @Override
    public void initData() {
        mTabPagers = new ArrayList<>();

        for (int i = 0;i<GlobalConstants.NEWS_URl.length;i++){
            BaseTabPager baseTabPager = new BaseTabPager(mActivity, i);
            mTabPagers.add(baseTabPager);
        }

        mNews_menu.setAdapter(new NewsTabPagerAdapter());
        mTabPageIndicator.setViewPager(mNews_menu);

    }

    class NewsTabPagerAdapter extends PagerAdapter {


        @Override
        public CharSequence getPageTitle(int position) {
            return GlobalConstants.NEWS_TITTLE[position];
        }

        @Override
        public int getCount() {
            return GlobalConstants.NEWS_URl.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view ==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            BaseTabPager baseTabPager = mTabPagers.get(position);


            View view = baseTabPager.initView();
            container.addView(view);
            baseTabPager.initData();

            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }


}
